package net.zjitc.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author Administrator
 * @Date 2020/5/15 8:43
 **/
@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*request对象功能
        3.获取请求体数据 （post请求才有请求体）
         */
        /*BufferedReader br = req.getReader();
        String line=null;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        if (br!=null){
            br.close();
        }*/

        /*获取请求参数
        get请求的参数在url里面
        post请求参数在请求体里面
         */
        String username = req.getParameter("username");
        Map<String, String[]> parameterMap = req.getParameterMap();
        String[] hobbies = req.getParameterValues("hobby");

        System.out.println("username: "+ username);
        System.out.println("hobby:");
        for (int i = 0; i < hobbies.length ; i++) {
            System.out.println(hobbies[i]);
        }
        System.out.println("所有的请求参数Map:");
        Iterator<String> iterator = parameterMap.keySet().iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            String[] values = parameterMap.get(key);
            System.out.println(key+ ":");
            for (String s:values){
                System.out.println(s);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }


}
